<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Questionare extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }   


    public function saveQuestionare($user)
    {
        $inputAnswers = array();
        $inputAnswers['user_id']=$user->id;        
        foreach($this->input->post() as $key => $val)
        {
            $inputAnswers[str_replace('-','_',$key)] = $val;
        } 
        $this->db->insert('questionare', $inputAnswers);
    }

    public function getComments($user_id,$user_admin){
        $this->db->from('comments');
        $this->db->where(array('user_id'=>$user_id,'user_level'=>$user_admin->user_level));
        $this->db->limit(1);
        $query = $this->db->get();
        //print_r($query->row());
        return $query->row();
    }

     public function insertRadnomesto($user)
    {
        $update_data = array(
            'user_radnomesto' => $this->input->post('upitnik-1-1'),
        );
        $user_id = $user->id;
        $this->db->where('id',$user_id);
        $this->db->update('users', $update_data);
        return true;
    }
 
    public function updateQuestionare($user,$user_admin)
    {    
        $inputAnswers = array();
        $commentsAnswers = array();
        //$inputAnswers['user_id']=$user->id; 
        //print_r($this->input->post());       
        foreach($this->input->post() as $key => $val)
        {
            if($key!='questionareUser' && $key!='user_id' && strpos($key, 'komentar')===false && $key!='process'){                
                $inputAnswers[str_replace('-','_',$key)] = $val;
            }
            if(strpos($key, 'komentar')!==false && $key!='komentar-pregledan' && $key!='process'){     
                //print_r($key. ' +  ' . $val );           
                $commentsAnswers[str_replace('-','_',$key)] = $val;            
            }
        }       
        //$user_id = $user->id;
        $this->db->where('user_id', $user)->update('questionare' ,$inputAnswers );
         //return true;        
        $comments = $this->getComments($user,$user_admin);
        if($comments){
            // update            
            $this->db->where(array('user_id'=>$user,'user_level'=>$user_admin->user_level))->update('comments' ,$commentsAnswers );
        }
        else {
            // insert
            $commentsAnswers['user_id']=$user;
            $commentsAnswers['user_level']=$user_admin->user_level;
            $this->db->insert('comments', $commentsAnswers);
        }
    }
    public function updateUserRadnomesto($user_id)
    {
        $update_data = array(
            'user_radnomesto' => $this->input->post('upitnik-1-1'),
        );        
        $this->db->where('id',$user_id);
        $this->db->update('users', $update_data);
        return true;
    }    

    public function acceptQuestionare($user_id){        
        $inputAnswers = array('status'=>2);
        $this->db->where('user_id', $user_id)->update('questionare' ,$inputAnswers );
    }

    public function verifieQuestionare($user_id){        
        $inputAnswers = array('status'=>3);
        $this->db->where('user_id', $user_id)->update('questionare' ,$inputAnswers );        
    }

    public function reviewQuestionare($user_id){        
        $inputAnswers = array('status'=>1);
        $this->db->where('user_id', $user_id)->update('questionare' ,$inputAnswers );
    }

    public function getQuestionare($user){
        $this->db->from('questionare');
        $this->db->where(array('user_id'=>$user->id));        
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->row();
    }
    public function getQuestionareold($user){
        $this->db->from('questionareold');
        $this->db->where(array('user_id'=>$user->id));        
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result();
    }
    public function getUserQuestionare($user_id){
        $this->db->from('questionare');
        $this->db->where(array('user_id'=>$user_id));        
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result();
    }
    public function getAllQuestionares(){
        $query = $this->db->get('questionare');
        return $query->result();
    }

    public function getOrganogramQuestionareSectorData($sector_id){
        $this->db->from('users');
        $this->db->join('questionare', 'users.id = questionare.user_id');
        $this->db->join('sectors', 'users.sector_id = sectors.id');
        $this->db->where(array('users.sector_id'=>$sector_id));
        $query = $this->db->get();
        return $query->result();
    }
   
       
}

?>